﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgradeSqlite.General
{
    /// <summary>
    /// 资源文件获取帮助类
    /// </summary>
    public class DraftHelper
    {
        #region 实例
        /// <summary>
        /// 实例
        /// </summary>
        private static DraftHelper _instance;
        /// <summary>
        /// 实例
        /// </summary>
        public static DraftHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DraftHelper();
                }
                return _instance;
            }
        }
        #endregion

        #region 外部方法

        #region 从资源中释放文件
        /// <summary>
        /// 从资源中释放文件
        /// </summary>
        /// <param name="filename">文件名</param>
        public void ReleaseFile(string filename)
        {
            string path = CodeHelper.CurrentPath;
            string filePath = Path.Combine(path, filename);
            CreateDirbyFileName(filePath);
            string vfileName = filename.Replace("\\", ".");

            if (!File.Exists(filePath))
            {
                byte[] buffer = GetFileResource(vfileName);
                if (buffer != null)
                {
                    FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
        }
        #endregion

        #region 从资源中释放文件
        /// <summary>
        /// 从资源中释放文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="path">路径</param>
        public void ReleaseFile(string filename, string path)
        {
            //string path = CodeHelper.CurrentPath;
            string filePath = Path.Combine(path, filename);
            CreateDirbyFileName(filePath);
            string vfileName = filename.Replace("\\", ".");

            if (!File.Exists(filePath))
            {
                byte[] buffer = GetFileResource(vfileName);
                if (buffer != null)
                {
                    FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
        }
        #endregion

        #region 获取文件资源
        /// <summary>
        /// 获取文件资源
        /// </summary>
        /// <param name="name">资源名称</param>
        /// <returns>文件资源流</returns>
        public byte[] GetFileResource(string name)
        {
            string basePath = string.Format("DbUpgradeSqlite.Draft.File.{0}", name);
            Assembly _assembly = Assembly.GetExecutingAssembly();
            using (Stream _stream = _assembly.GetManifestResourceStream(basePath))
            {
                if (_stream == null)
                {
                    return null;
                }
                else
                {
                    byte[] b = new byte[_stream.Length];
                    _stream.Read(b, 0, b.Length);
                    return b;
                }
            }
        }
        #endregion

        #region 获取文件内容
        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="name">资源名称</param>
        /// <returns>模板文件</returns>
        public string GetStringResource(string fileName)
        {
            string basePath = string.Format("DbUpgradeSqlite.Draft.File.{0}", fileName);
            Assembly _assembly = Assembly.GetExecutingAssembly();
            using (Stream _stream = _assembly.GetManifestResourceStream(basePath))
            {
                if (_stream == null)
                {
                    return string.Empty;
                }
                else
                {
                    StreamReader _textSr = new StreamReader(_stream);
                    string sqlTemp = _textSr.ReadToEnd();
                    return sqlTemp;
                }
            }
        }
        #endregion

        #region 创建目录(传入一个不带文件名的目录路径)
        /// <summary>
        /// 创建目录(传入一个不带文件名的目录路径)
        /// </summary>
        /// <param name="dir">目录路径</param>
        public void CreateDirbyDir(string dir)
        {
            string[] dirs = dir.Split('\\');
            string vdir = string.Empty;
            int vlength = 0;
            if (string.IsNullOrEmpty(dirs[dirs.Length - 1]))
            {
                vlength = dirs.Length - 1;
            }
            else
            {
                vlength = dirs.Length;
            }
            for (int i = 0; i < vlength; i++)
            {
                vdir += (dirs[i] + "\\");
                if (!Directory.Exists(vdir))
                {
                    Directory.CreateDirectory(vdir);
                }
            }
        }
        #endregion

        #region 创建目录(传入一个带文件名的目录路径)
        /// <summary>
        /// 创建目录(传入一个带文件名的目录路径)
        /// </summary>
        /// <param name="dir">目录路径</param>
        public void CreateDirbyFileName(string fileName)
        {
            string inidir = Path.GetDirectoryName(fileName);
            string[] dirs = inidir.Split('\\');
            string vdir = string.Empty;
            int vlength = 0;
            if (string.IsNullOrEmpty(dirs[dirs.Length - 1]))
            {
                vlength = dirs.Length - 1;
            }
            else
            {
                vlength = dirs.Length;
            }
            for (int i = 0; i < vlength; i++)
            {
                vdir += (dirs[i] + "\\");
                if (!Directory.Exists(vdir))
                {
                    Directory.CreateDirectory(vdir);
                }
            }
        }
        #endregion

        #region 释放初始文件
        /// <summary>
        /// 释放初始文件
        /// </summary>
        public void ReleaseBasicFile()
        {
            try
            {
                //Log.WriteDebug("1");
                //string path = CodeHelper.CurrentDocPath;
                ReleaseFile("dbdata.db");
                //ReleaseFile("dbupgrade.db");
                //Log.WriteDebug("2");
                //ReleaseFile("System.Data.SQLite.dll");
                //ReleaseFile("x64\\SQLite.Interop.dll");
                //ReleaseFile("x86\\SQLite.Interop.dll");
                //ReleaseFile("fail.wav", path);
                //ReleaseFile("success.wav", path);
                //ReleaseFile("Sqlite.ManagedDataAccess.dll");
            }
            catch (Exception ex)
            {
                Log.WriteException(ex);
            }
        }
        #endregion

        #endregion
    }
}
